$(function() {
	var imgURL;
	//获取图片并展示
	$("#picFile").change(function(event) {
		var files = event.target.files,
			file;
		if (files && files.length > 0) {
			file = files[0];
			if (file.size > 1024 * 1024 * 2) {
				alert('图片大小不能超过 2MB!');
				return false;
			}
			var URL = window.URL || window.webkitURL;
			imgURL = URL.createObjectURL(file);
			$("#img").attr("src", imgURL);
			$("#imgDiv").show();
			$("#originalText").css("display", "none");
		}
	});

	//获取密码列表
	$.ajax({
		type: "POST",
		url: "http://127.0.0.1:8080/key/findAllKeyByLoginUser",
		dataType: "json",
		xhrFields: {
			withCredentials: true
		},
		success: function(data) {
			var code = data.code;
			if (code == 'success') {
				var ksys = data.data;
				var html = "<option value=''></option>";
				ksys.forEach(function(key) {
					html += "<option value='" + key.password + "'>" + key.password + "</option>";
				});
				$("#squareSelect").html(html);
			} else {
				flag = false;
				notifyMsg(data.msg);
			}
		}
	})

	//是否图片的效果
	$('input[type=radio][name=isPic]').change(function() {
		if (this.value == 'Y') {
			$("#picFile").css("display", "");
		} else if (this.value == 'N') {
			$("#picFile").css("display", "none");
		}
	});

	//加密
	$("#encrypt").click(function() {
		var isPic = $('input:radio[name="isPic"]:checked').val();
		var password = $("#squareSelect").val();
		if (password == '') {
			notifyMsg("加密密码不能为空，请选择");
			return;
		}
		if ("N" == isPic) { //文字加密
			var originalText = $("#originalText").val();
			if (originalText == '') {
				notifyMsg("加密内容不能为空，请填写");
				return;
			}
			$.ajax({
				type: "POST",
				url: "http://127.0.0.1:8080/security/encryptTxt",
				dataType: "json",
				data: {
					"password": password,
					"originalText": originalText
				},
				xhrFields: {
					withCredentials: true
				},
				success: function(data) {
					if (data.code == 'success') {
						$("#cipherText").text(data.data);
					} else {
						notifyMsg(data.msg);
					}
				}
			})
		} else { //图片加密
			var node = $("#uploadFile");
			var fileName = node.val();
			if (fileName == '') {
				notifyMsg("请选择加密的图片");
				return;
			}
			var formdata = new FormData();
			formdata.append('file', node.get(0).files[0]);
			formdata.append("password", password);
			$.ajax({
				url: "http://127.0.0.1:8080/security/encryptPic",
				method: "post",
				data: formdata,
				xhrFields: {
					withCredentials: true
				},
				contentType: false,
				processData: false,
				dataType: "json",
				success: function(data) {
					if (data.code == 'success') {
						$("#cipherText").text(data.data);
					} else {
						notifyMsg(data.msg);
					}
				}
			})
		}
	})

	//解密
	$("#decrypt").click(function() {
		var isPic = $('input:radio[name="isPic"]:checked').val();
		var password = $("#squareSelect").val();
		if (password == '') {
			notifyMsg("解密密码不能为空，请选择");
			return;
		}
		if ("N" == isPic) { //文字加密
			var cipherText = $("#originalText").val();
			if (originalText == '') {
				notifyMsg("解密内容不能为空，请填写");
				return;
			}
			$.ajax({
				type: "POST",
				url: "http://127.0.0.1:8080/security/decryptTxt",
				dataType: "json",
				data: {
					"password": password,
					"cipherText": cipherText
				},
				xhrFields: {
					withCredentials: true
				},
				success: function(data) {
					if (data.code == 'success') {
						$("#cipherText").text(data.data);
					} else {
						notifyMsg(data.msg);
					}
				}
			})
		} else { //图片加密
			var cipherText = $("#originalText").val();
			if (originalText == '') {
				notifyMsg("解密内容不能为空，请填写");
				return;
			}
			$.ajax({
				type: "POST",
				url: "http://127.0.0.1:8080/security/decryptPic",
				dataType: "json",
				data: {
					"password": password,
					"content": cipherText
				},
				xhrFields: {
					withCredentials: true
				},
				success: function(data) {
					$("#imgDe").attr("src", "http://127.0.0.1:8080" + data.data);
					$("#imgDivDe").css("display", "");
					$("#cipherText").css("display", "none");
				}
			})
		}
	})
})
